Euler Problem 81

In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in red and is equal to 2427.

[131]   673 234 103 18
[201]   [96]    [342]   965 150
630 803 [746]   [422]   111
537 699 497 [121]   956
805 732 524 [37]    [331]

In [1]:
infile = open('data/p081_matrix.txt', 'r');
matrix = []
for line in infile:
    row = list(map(int, line.split(',')))
    matrix += [row]

for i in range(79):
    matrix[0][i+1] += matrix[0][i]
    matrix[i+1][0] += matrix[i][0]

for i in range(79):
    for j in range(79):
        matrix[i+1][j+1] += min(matrix[i][j+1], matrix[i+1][j])
print(matrix[79][79])


427337

In [ ]: